package com.BEASTMODE.libs;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import com.BEASTMODE.games.SpeedPong.R;

import android.content.Context;
import android.opengl.GLES20;
import android.os.SystemClock;


public class Triangle {
	
	private FloatBuffer vertexBuffer;
	private FloatBuffer texCoordBuffer;
	private CShader shaderProgram;
	private int numVerts;
	
	public void Init(float[] position, float[] texCoords, CShader usingShader)
	{
		ByteBuffer vbb = ByteBuffer.allocateDirect(position.length * 4);
		
		numVerts = position.length / 3;
		
		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(position);
		vertexBuffer.position(0);
		
		ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
		
		tbb.order(ByteOrder.nativeOrder());
		texCoordBuffer = tbb.asFloatBuffer();
		texCoordBuffer.put(texCoords);
		texCoordBuffer.position(0);
		
		shaderProgram = usingShader;
	}
	
	public void Draw(float[] mvpMat, int texID)
	{
		shaderProgram.Activate();
		
		
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texID);
		
		GLES20.glVertexAttribPointer(shaderProgram.Position(), 3, GLES20.GL_FLOAT, false, 12, vertexBuffer);
		GLES20.glEnableVertexAttribArray(shaderProgram.Position());
		
		GLES20.glVertexAttribPointer(shaderProgram.TexCoord(), 3, GLES20.GL_FLOAT, false, 8, texCoordBuffer);
		GLES20.glEnableVertexAttribArray(shaderProgram.TexCoord());
		
		GLES20.glUniformMatrix4fv(shaderProgram.MVPMat(), 1, false, mvpMat, 0);
		GLES20.glUniform1i(shaderProgram.Texture0(), 0);
		
		GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
	}
}
